package com.qf.bank.redis.config;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * @author 惠佳旭
 * @date 2019/11/27
 */
public class RedisUtilsSerializable implements RedisSerializer<Object> {

  private Converter<Object, byte[]> serializingConverter = new SerializingConverter();
  private Converter<byte[], Object> deserializingConverter = new DeserializingConverter();
  private final byte[] EMPTY = new byte[0];
  /**
   * 序列化
   *
   * @auther 惠佳旭
   * @param
   * @return byte[]
   * @date 2019/11/27
   */
  @Override
  public byte[] serialize(Object o) throws SerializationException {
    if (o == null) {
      return EMPTY;
    }
    return serializingConverter.convert(o);
  }

  /**
   * 反序列化
   *
   * @auther 惠佳旭
   * @param
   * @return java.lang.Object
   * @date 2019/11/27
   */
  @Override
  public Object deserialize(byte[] bytes) throws SerializationException {
    if (bytes != null) {
      return deserializingConverter.convert(bytes);
    }
    return null;
  }
}
